﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;
using Telerik.Web.UI;

public partial class Menus_Posts : BaseMenuPage
{
    public override Role.Roles[] AllowedRoles
    {
        get { return new Role.Roles[] { Role.Roles.Developer, Role.Roles.ContentEditor, Role.Roles.ContentViewer }; }
    }

    public override Role.Roles[] EditRoles
    {
        get { return new Role.Roles[] { Role.Roles.Developer, Role.Roles.ContentEditor }; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lnkEdit.Visible = lnkSave.Visible = lnkCancel.Visible = false;
            if (CurrentMenu != null)
            {
                title.DataSource = new Acms.Bll.Menu[] { CurrentMenu };
                title.DataBind();

                dsMenu.SiteId = dsRelative.SiteId = AcmsContext.SiteId;
                dsMenu.MenuName = CurrentMenu.Name;
                dsMenu.CurrentSiteVersion = dsRelative.CurrentSiteVersion = siteVersionTab.SiteVersion;

                if (!string.IsNullOrEmpty(CurrentMenu.ParentPath) && !CurrentMenu.ParentPath.StartsWith("~/"))
                {
                    pnlDisplay.Visible = false;
                    pnlSelRelative.Visible = true;
                    lblRelative.Visible = true;
                }
                else
                {
                    pnlDisplay.Visible = true;
                    pnlSelRelative.Visible = false;
                    lblRelative.Visible = false;

                }

            }
        }

    }

    protected void siteVersionTab_SelectedVersionChanged(object sender, EventArgs e)
    {
        dsMenu.CurrentSiteVersion = dsRelative.CurrentSiteVersion = siteVersionTab.SiteVersion;
        if (pnlSelRelative.Visible)
        {
            treeRelative.DataBind();
        }
        else
        {
            treeMenu.DataBind();
        }
    }

    protected RadTreeNode CreateNode(Post post)
    {
        if (post != null)
        {

            RadTreeNode node = new RadTreeNode();
            node.Value = post.PostGuid.ToString();
            node.Text = post.DisplayName;
            return node;
        }
        else
        {
            return null;
        }
    }


    protected void treeRelative_NodeClick(object sender, Telerik.Web.UI.RadTreeNodeEventArgs e)
    {
        Guid id = Guid.Empty;
        try
        {
            id = new Guid(e.Node.Value);
        }
        catch
        {
        }
        if(id != Guid.Empty)
        {
            Post tmp = Post.GetByPostGuid(AcmsContext.SiteId, id, siteVersionTab.SiteVersion);
            if(tmp != null)
            {
                lblRelative.Text = string.Format("Menu ralative to {0}", tmp.Url);
                dsMenu.CurrentRelativeUrl = tmp.Url;
                pnlDisplay.Visible = true;
                pnlSelRelative.Visible = false;
            }
        }
    }

    protected void treeMenu_DataBound(object sender, EventArgs e)
    {
        if (treeMenu.Nodes.Count > 0)
        {
            treeMenu.Nodes[0].Expanded = true;
            foreach (RadTreeNode node in treeMenu.Nodes[0].Nodes)
            {
                Guid postGuid = new Guid(node.Value);
                List<Post> children = Post.GetAllByParentGuid(AcmsContext.SiteId, postGuid, siteVersionTab.SiteVersion);
                if (children != null && children.Count > 0)
                {
                    node.ExpandMode = TreeNodeExpandMode.ServerSide;
                }
            }
        }
    }

    protected void treeMenu_NodeExpand(object sender, RadTreeNodeEventArgs e)
    {
        e.Node.ExpandMode = TreeNodeExpandMode.ClientSide;
        Guid postGuid = new Guid(e.Node.Value);
        List<Post> children = Post.GetAllByMenuIdAndParentGuid(AcmsContext.SiteId, MenuId, postGuid, siteVersionTab.SiteVersion);
        foreach (Post child in children)
        {
            RadTreeNode tmp = CreateNode(child);
            if (tmp != null)
            {
                if (Post.GetAllByMenuIdAndParentGuid(AcmsContext.SiteId, MenuId, child.PostGuid, siteVersionTab.SiteVersion).Count > 0)
                {
                    tmp.ExpandMode = TreeNodeExpandMode.ServerSide;
                }
                e.Node.Nodes.Add(tmp);
            }
        }
    }

    protected void treeRelative_DataBound(object sender, EventArgs e)
    {
        if (treeRelative.Nodes.Count > 0)
        {
            treeRelative.Nodes[0].Expanded = true;
            foreach (RadTreeNode node in treeRelative.Nodes[0].Nodes)
            {
                Guid postGuid = new Guid(node.Value);
                List<Post> children = Post.GetAllByParentGuid(AcmsContext.SiteId, postGuid, siteVersionTab.SiteVersion);
                if (children != null && children.Count > 0)
                {
                    node.ExpandMode = TreeNodeExpandMode.ServerSide;
                }
            }
        }
    }

    protected void treeRelative_NodeExpand(object sender, RadTreeNodeEventArgs e)
    {
        e.Node.ExpandMode = TreeNodeExpandMode.ClientSide;
        Guid postGuid = new Guid(e.Node.Value);
        List<Post> children = Post.GetAllByParentGuid(AcmsContext.SiteId, postGuid, siteVersionTab.SiteVersion);
        foreach (Post child in children)
        {
            RadTreeNode tmp = CreateNode(child);
            if (tmp != null)
            {
                if (child.GetChildren(siteVersionTab.SiteVersion).Count > 0)
                {
                    tmp.ExpandMode = TreeNodeExpandMode.ServerSide;
                }
                e.Node.Nodes.Add(tmp);
            }
        }
    }

    private Post _SelectedPost;
    protected Post SelectedPost
    {
        get
        {
            if (_SelectedPost == null)
            {
                Guid postGuid = new Guid(treeMenu.SelectedNode.Value);
                _SelectedPost = Post.GetByPostGuid(AcmsContext.SiteId, postGuid, siteVersionTab.SiteVersion);
            }
            return _SelectedPost;
        }
    }



    protected void treeMenu_NodeClick(object sender, RadTreeNodeEventArgs e)
    {
        if (SelectedPost != null)
        {
            colChildren.Visible = true;
            colSiblings.Visible = true;

            lnkEdit.Visible = User.HasRole(EditRoles);
            lnkSave.Visible = lnkCancel.Visible = false;

            cblSiblings.Enabled = false;
            cblChildren.Enabled = false;

            cblChildren.DataBind();
            cblSiblings.DataBind();
        }
    }

    protected void cblChildren_DataBinding(object sender, EventArgs e)
    {
        cblChildren.DataSource = Post.GetAllByParentGuid(AcmsContext.SiteId, SelectedPost.PostGuid, siteVersionTab.SiteVersion);
    }

    protected void cblChildren_DataBound(object sender, EventArgs e)
    {
        if (cblChildren.Items.Count > 0)
        {
            if (treeMenu.SelectedNode.ExpandMode != TreeNodeExpandMode.ClientSide)
            {
                Guid parentId = new Guid(treeMenu.SelectedValue);
                List<Post> posts = Post.GetAllByMenuIdAndParentGuid(AcmsContext.SiteId, MenuId, parentId, siteVersionTab.SiteVersion);
                foreach (Post post in posts)
                {
                    RadTreeNode tmp = CreateNode(post);
                    if (tmp != null)
                    {
                        if (Post.GetAllByMenuIdAndParentGuid(AcmsContext.SiteId, MenuId, post.PostGuid, siteVersionTab.SiteVersion).Count > 0)
                        {
                            tmp.ExpandMode = TreeNodeExpandMode.ServerSide;
                        }

                        treeMenu.SelectedNode.Nodes.Add(tmp);
                    }
                }
                treeMenu.SelectedNode.ExpandMode = TreeNodeExpandMode.ClientSide;
            }

            colChildren.Visible = true;
            SetChildrenCheckboxValues();
        }
        else
        {
            colChildren.Visible = false;
        }

    }

    protected void cblSiblings_DataBinding(object sender, EventArgs e)
    {
        if (SelectedPost.ParentGuid != null)
        {
            cblSiblings.DataSource = Post.GetAllByParentGuid(AcmsContext.SiteId, SelectedPost.ParentGuid.Value, siteVersionTab.SiteVersion);
        }
        else
        {
            cblSiblings.DataSource = new Post[0];
        }
    }

    protected void cblSiblings_DataBound(object sender, EventArgs e)
    {
        if (cblSiblings.Items.Count > 0)
        {
            colSiblings.Visible = true;
            SetSiblingsCheckboxValues();

        }
        else
        {
            colSiblings.Visible = false;
        }
    }

    public void SetSiblingsCheckboxValues()
    {
        foreach (ListItem item in cblSiblings.Items)
        {
            if (treeMenu.SelectedNode.ParentNode == null)
            {
                item.Selected = treeMenu.Nodes.FindNodeByValue(item.Value) != null;
            }
            else
            {
                item.Selected = treeMenu.SelectedNode.ParentNode.Nodes.FindNodeByValue(item.Value) != null;
            }
        }
    }

    public void SetChildrenCheckboxValues()
    {
        foreach (ListItem item in cblChildren.Items)
        {
            item.Selected = treeMenu.SelectedNode.Nodes.FindNodeByValue(item.Value) != null;
        }
    }

    protected void lnkSave_Click(object sender, EventArgs e)
    {
        List<PostMenu> updates = new List<PostMenu>();

        RadTreeNodeCollection sibling = treeMenu.Nodes;
        if(treeMenu.SelectedNode.ParentNode != null)
        {
            sibling = treeMenu.SelectedNode.ParentNode.Nodes;
        }
        RadTreeNodeCollection children = treeMenu.SelectedNode.Nodes;

        foreach (ListItem item in cblSiblings.Items)
        {
            bool update = false;

            if (item.Selected && sibling.FindNodeByValue(item.Value) == null)
            {
                update = true;
            }
            else if (!item.Selected && sibling.FindNodeByValue(item.Value) != null)
            {
                update = true;
            }

            if (update)
            {
                bool found = false;
                Post tmpPost = Post.GetByPostGuid(AcmsContext.SiteId, new Guid(item.Value), siteVersionTab.SiteVersion);
                List<PostMenu> postMenus = PostMenu.GetAllByPostId(tmpPost.Id);
                foreach(PostMenu postMenu in postMenus)
                {
                    if (postMenu.MenuId == MenuId)
                    {
                        found = true;
                        postMenu.Included = item.Selected;
                        updates.Add(postMenu);
                        break;
                    }
                }
                if (!found)
                {
                    PostMenu tmp = PostMenu.CreateInstance(tmpPost.Id, MenuId, true);
                    updates.Add(tmp);
                }
            }
        }

        foreach (ListItem item in cblChildren.Items)
        {
            bool update = false;
            if (item.Selected && children.FindNodeByValue(item.Value) == null)
            {
                update = true;
            }
            else if (!item.Selected && children.FindNodeByValue(item.Value) != null)
            {
                update = true;
            }
            if (update)
            {
                bool found = false;
                Post tmpPost = Post.GetByPostGuid(AcmsContext.SiteId, new Guid(item.Value), siteVersionTab.SiteVersion);
                List<PostMenu> postMenus = PostMenu.GetAllByPostId(tmpPost.Id);
                foreach (PostMenu postMenu in postMenus)
                {
                    if (postMenu.MenuId == MenuId)
                    {
                        found = true;
                        postMenu.Included = item.Selected;
                        updates.Add(postMenu);
                        break;
                    }
                }
                if (!found)
                {
                    PostMenu tmp = PostMenu.CreateInstance(tmpPost.Id, MenuId, true);
                    updates.Add(tmp);
                }
            }
        }

        if (updates.Count > 0)
        {
            updates[0].BeginTransaction();
            foreach (PostMenu tmp in updates)
            {
                if (tmp.TransactionKey == Guid.Empty)
                {
                    tmp.UseTransaction(updates[0].TransactionKey);
                }
                tmp.Update();
            }
            updates[0].CommitTransaction();

            //rebind tree
            if (treeMenu.SelectedNode.ParentNode == null)
            {
                //top level so rebind whole thing
                treeMenu.DataBind();
                if (treeMenu.Nodes.Count > 0)
                {
                    treeMenu.Nodes[0].Selected = true;
                }
            }
            else
            {
                RadTreeNode parent = treeMenu.SelectedNode.ParentNode;
                string selectedValue = treeMenu.SelectedNode.Value;
                parent.Nodes.Clear();
                RadTreeNodeEventArgs tmpArgs = new RadTreeNodeEventArgs(parent);
                treeMenu_NodeExpand(treeMenu, tmpArgs);
                foreach(RadTreeNode node in parent.Nodes)
                {
                    if (node.Value == selectedValue)
                    {
                        node.Selected = true;
                        tmpArgs = new RadTreeNodeEventArgs(node);
                        treeMenu_NodeExpand(treeMenu, tmpArgs);
                        node.Expanded = true;
                    }
                }
            }
            //rebing check box lists
            cblChildren.DataBind();
            cblSiblings.DataBind();

        }

        cblSiblings.Enabled=cblChildren.Enabled = false;

        lnkEdit.Visible = true;
        lnkSave.Visible = lnkCancel.Visible = false;
    }

    protected void lnkCancel_Click(object sender, EventArgs e)
    {
        cblChildren.Enabled = false;
        cblSiblings.Enabled = false;
        lnkEdit.Visible = true;
        lnkSave.Visible = lnkCancel.Visible = false;

        SetSiblingsCheckboxValues();
        SetChildrenCheckboxValues();
    }

    protected void lnkEdit_Click(object sender, EventArgs e)
    {
        cblSiblings.Enabled = true;
        cblChildren.Enabled = true;
        lnkEdit.Visible = false;
        lnkSave.Visible = lnkCancel.Visible = true;
    }
}
